//
//  NumView.m
//  KKMY2_U
//
//  Created by 黄磊 on 14-4-14.
//  Copyright (c) 2014年 ;;. All rights reserved.
//

#import "NumView.h"

#define DefaultViewHeight 20

@interface NumView ()

@property (nonatomic, strong) UILabel *lblNum;
@property (nonatomic, strong) UIFont *curFont;
@property (nonatomic, assign) CGFloat curPadding;
@property (nonatomic, assign) int curLength;
@property (nonatomic, assign) CGFloat wordLength;

@end

@implementation NumView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self settingView];
    }
    return self;
}

- (void)awakeFromNib
{
    [self settingView];
}

- (void)settingView
{
    CGPoint center = self.center;
    CGRect rect = self.frame;
    _height = rect.size.height;
    if (self.tag == 0) {
        _height = DefaultViewHeight;
    }
    rect.size.height = _height;
    rect.size.width = _height;
    [self setFrame:rect];
    [self setCenter:center];
    _curFont = [UIFont systemFontOfSize:_height * 3 / 4];
    _curPadding = _height * 1 / 4;
    self.layer.cornerRadius = _height / 2;
    self.layer.masksToBounds = YES;
    [self setBackgroundColor:[UIColor redColor]];
    _lblNum = [[UILabel alloc] initWithFrame:self.bounds];
    [_lblNum setBackgroundColor:[UIColor clearColor]];
    [_lblNum setTextAlignment:NSTextAlignmentCenter];
    [_lblNum setFont:_curFont];
    [_lblNum setTextColor:[UIColor whiteColor]];
    [_lblNum setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
    [self addSubview:_lblNum];
    
    [self setHidden:YES];
}

- (void)setHeight:(CGFloat)height
{
    CGPoint center = self.center;
    CGRect rect = self.frame;
    _height = height;
    rect.size.height = _height;
    rect.size.width = _height;
    [self setFrame:rect];
    [self setCenter:center];
    _curFont = [UIFont systemFontOfSize:_height * 3 / 4];
    _curPadding = _height * 1 / 4;
    [_lblNum setFont:_curFont];
}

#pragma mark - Public

- (void)addNum
{
    [self addNum:1];
}

- (void)addNum:(int)num
{
    [self setCurNum:_curNum + num];
}

- (void)subNum
{
    [self subNum:1];
}

- (void)subNum:(int)num
{
    [self setCurNum:_curNum - num];
}

- (void)setCurNum:(int)curNum
{
    if (_curNum == curNum) {
        return;
    }
    if (curNum < 0) {
        curNum = 0;
    }
    _curNum = curNum;
    if (_curNum == 0) {
        [self setHidden:YES];
        return;
    }
    [self setHidden:NO];
    NSString *strNum = [NSString stringWithFormat:@"%d", curNum];
    [_lblNum setText:strNum];
    CGSize size = textSizeWithFont(strNum, _curFont);
    [self resizeSelfWith:size.width];
}

- (void)resizeSelfWith:(CGFloat)length
{
    CGFloat width = length + 2 * _curPadding;
    if (width < _height) {
        width = _height;
    }
    CGPoint center = self.center;
    CGRect rect = self.bounds;
    rect.size.width = width;
    [self setFrame:rect];
    [self setCenter:center];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
